<?php

/**
 * 简单桶排序(也叫计数排序): 以空间换时间的方式.
 */
function BucketSort($numbers) {
    $bucketList = [];
    $maxValue = max($numbers);
    for ($i = 0; $i <= $maxValue; $i++) {
        $bucketList[$i] = 0;
    }
    foreach ($numbers as $n) {
        $bucketList[$n]++;
    }
    $sortList = [];
    foreach ($bucketList as $k => $v) {
        if ($v > 0) {
            for (; $v > 0; $v--) {
                $sortList[] = $k;
            }
        }
    }
    return implode(', ', $sortList);
}

$numbers = [49, 38, 65, 97, 76, 13, 27, 49];
echo "原始数据: " . implode(', ', $numbers), PHP_EOL;
echo "桶排序结果: " . BucketSort($numbers), PHP_EOL;
//13, 27, 38, 49, 49, 65, 76, 97